/*
 * @Author: 杨涛 2749552387@qq.com
 * @Date: 2024-10-24 15:27:33
 * @LastEditors: 杨涛 2749552387@qq.com
 * @LastEditTime: 2024-10-25 16:17:26
 * @FilePath: \node-modbus\db\mySqlDb.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mysql = require('mysql2');
const { MYSQL_CONFIG,servicePort } = require('../config/db');
const port = servicePort;
const connection = mysql.createConnection(MYSQL_CONFIG)

// 连接数据库
connection.connect((error) => {
    if (error) {
        console.error('连接失败：', error);
    } else {
        console.log('数据库连接成功！');
    }
});

const execSQL = (sql,params) => {
    const promise =  new Promise((resolve, reject) => {
        connection.query(sql,params, (error, result) => {
            if (error) {
                return reject(error);
            }
            resolve(result);
        })
    })
    return promise;
}

module.exports = {
    port,
    execSQL
};